#region (c) 2014 Nguyễn, M. Hải - All rights reserved
/*
BLADE VIEW ENGINE

The MIT License (MIT)
Copyright (c) 2014 Nguyễn, M. Hải

Permission is hereby granted, free of charge, to any person obtaining a copy of this 
software and associated documentation files (the "Software"), to deal in the Software 
without restriction, including without limitation the rights to use, copy, modify, merge
, publish, distribute, sublicense, and/or sell copies of the Software, and to permit 
persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or
 substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
DEALINGS IN THE SOFTWARE.
*/
#endregion

#region Record of Change
//             							CHANGE HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | DEVELOPER  | DESCRIPTION                                              |
// -------------------------------------------------------------------------------------
// | 30-Apr-14 | HaiNM      | First creation.                                          |
// -------------------------------------------------------------------------------------
//
#endregion

#region Record of Audit
//             							REVIEW HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | REVIEWER   | NOTES                                                    |
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
//
#endregion

#region Using directives
using System;
using System.IO;

using Thn;
using Thn.Net;
using Thn.Net.Http;
using Thn.Web.Mvc;
#endregion

namespace Thn.Web.Blade
{
    /// <summary>
    /// 
    /// </summary>
#if DEBUG || TESTING
    public
#else
    internal
#endif
 class BladeFileRequestHandler : IHttpRequestHandler
    {
        #region Fields
        #endregion

        #region Properties

        #region File Extension
        private string mFileExtension = ".cshtml";
        /// <summary>
        /// Gets or sets the required file extension.
        /// <para>Default is cshtml</para>
        /// </summary>
        public string FileExtension
        {
            get { return mFileExtension; }
            set { mFileExtension = value; }
        }
        #endregion

        #region View Renderer
        private IViewRenderer mViewRenderer;
        /// <summary>
        /// Gets or sets reference to the renderer responsible for rendering views.
        /// </summary>
        public IViewRenderer ViewRenderer
        {
            get { return mViewRenderer; }
            set { mViewRenderer = value; }
        }
        #endregion

        #endregion

        #region Render View
        /// <summary>
        /// Render a view template with the provided data model.
        /// </summary>
        /// <param name="viewName">Name of the view to be rendered.</param>
        /// <param name="model">The data model object to be used by the view.</param>
        protected IHttpResponse RenderView(string viewName, IHttpRequestHandlingContext context, object model = null)
        {
            HttpResponse result = new HttpResponse();
            RenderArgs args = new RenderArgs();
            args.ViewName = viewName;
            args.Model = model;
            args.Writer = result.Body.Writer;
            args.Context = context;
            mViewRenderer.Render(args);
            return result;
        }
        #endregion

        #region IHttpRequestHandler Members

        #region Process
        void IHttpRequestHandler.Process(IHttpRequestHandlingContext context)
        {
            var url = context.Request.Path;
            var fileExtension = Path.GetExtension(url);

            if (fileExtension == mFileExtension)
            {
                #region Matched required extension

                var viewName = url.Substring(0, url.Length - fileExtension.Length);

                context.Response = RenderView(viewName, context);

                #endregion
            }
            else
            {
                #region Extension not matched -> quit
                #endregion
            }
        }
        #endregion

        #endregion

        #region Constructors
        /// <summary>
        /// Create a new instance.
        /// </summary>
        public BladeFileRequestHandler()
            : base()
        { }
        #endregion
    }
}
